package com.engine.dffd.sync.action;

import com.alibaba.fastjson.JSONObject;
import com.engine.addons.common.entity.ResultMsg;
import com.engine.addons.common.util.LogUtil;
import com.engine.addons.ecology.common.entity.RequestInfoBean;
import com.engine.addons.ecology.common.util.RequestInfoUtil;
import com.engine.addons.init.api.service.APIDataInitService;
import com.engine.addons.init.api.service.impl.APIDataInitServiceImpl;
import com.engine.addons.init.dataProcess.entity.DataFormTable;
import com.engine.addons.init.dataProcess.entity.Map4Json;
import com.engine.dffd.util.DffdSyncDataUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

// 叶片目标成本发起
public class YpmbcbfqAction implements Action {
    private static final Log log = LogFactory.getLog(YpmbcbfqAction.class);
    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("处理叶片目标成本发起接口，请求参数为："+requestInfo.getRequestid());
        try{
            RequestInfoBean rib = new RequestInfoBean(requestInfo);
            Map<String, Object> mainMap = rib.getMainMap();
            log.info("requestInfo:"+ JSONObject.toJSONString(rib));
            List<Map4Json> map4JsonList = new ArrayList<>();
            map4JsonList.add(RequestInfoUtil.requestInfoToMap4Json(rib));

            String confName = "ypmbcbfqSync";

            //处理数据
            APIDataInitService as = APIDataInitServiceImpl.getInstance();
            ResultMsg<DataFormTable> apiTableResultMsg = as.processData(null, map4JsonList, confName);
            if (!apiTableResultMsg.isStatus()) {
                requestInfo.getRequestManager().setMessagecontent("叶片目标成本发起出错：" + apiTableResultMsg.getText());
                return Action.FAILURE_AND_CONTINUE;
            }
            log.info("apiTableResultMsg:"+JSONObject.toJSONString(apiTableResultMsg));
            JSONObject sendJson = DffdSyncDataUtil.covertTableToJson(apiTableResultMsg.getObject());
            JSONObject resJson = new JSONObject();
            resJson.put("DATA", sendJson);
            resJson.put("UUID", rib.getRequestid());
            resJson.put("IFNAME", "C0031");
            resJson.put("SYS_ID", "OA");
            ResultMsg<String> apiReturnMsg = DffdSyncDataUtil.callSync(resJson.toJSONString(), "ypmbcbfqSync");
            if (!apiReturnMsg.isStatus()) {
                requestInfo.getRequestManager().setMessagecontent("叶片目标成本发起出错：" + apiReturnMsg.getText());
                return Action.FAILURE_AND_CONTINUE;
            }
            // 处理返回结果
            JSONObject resJsonObj = JSONObject.parseObject(apiReturnMsg.getObject());
            String ZMBCBBH = resJsonObj.getString("ZMBCBBH");
            if (ZMBCBBH == null || ZMBCBBH.isEmpty()) {
                requestInfo.getRequestManager().setMessagecontent("处理叶片测算发起接口出错：返回结果中未包含ZMBCBBH");
                return Action.FAILURE_AND_CONTINUE;
            }
            log.info("处理叶片目标成本发起接口成功，返回结果中ZMBCBBH为："+ ZMBCBBH );
            // 更新OA数据
            RecordSet set = new RecordSet();
            // 正式环境 todo  formtable_main_1711
//            String sql ="update formtable_main_861 set ZMBCBBH = '"+ZMBCBBH+"' where requestId = '"+requestInfo.getRequestid()+"'";
            String sql ="update formtable_main_1711 set ZMBCBBH = '"+ZMBCBBH+"' where requestId = '"+requestInfo.getRequestid()+"'";
            set.executeUpdate(sql);
            log.info("处理叶片目标成本发起接口，返回结果为："+JSONObject.toJSONString(resJsonObj));
            return Action.SUCCESS;
        } catch (Exception e) {
            log.error("处理叶片目标成本接口出错" + e);
            requestInfo.getRequestManager().setMessagecontent("处理叶片目标成本接口出错" + e.getMessage());
        }
        return Action.FAILURE_AND_CONTINUE;
    }
}
